/*
 * @Description: 
 * @Author: xuesong
 * @Date: 2021-06-08 20:17:02
 * @LastEditTime: 2022-04-19 11:42:46
 * @LastEditors: xuesong
 * @Reference: 
 */


#include "OSAL.h"
#include "ll.h"
#include "clock.h"
#include "log.h"
#include "peripheral.h"
#include "halPeripheral.h"
#include "user_prcess.h"
#include "simpleBLEPeripheral.h"
// #include "multi.h"
// #include "multi_role.h"
// #include "CH456IF.h"
#include "user_data.h"
#include "Display.h"
#include "user_Bz.h"
#include "BatteryMeasure.h"
// #include "AdcMeasure.h"
#include "crc16.h"
#include "userApp.h"
// #include "cs1259B.h"
// #include "cs1237.h"
// #include "s_sample.h"
// #include "s_weight.h"
// #include "CoffeeHandleMode.h"
// #include "s_calibration.h"
//-----------------------------------------------------------
UserFlag_t UserFlag={
    .flagByte = 0,
};
User_Process_t UserProcess={
    .ProcessFlag.flagByte = 0,
    .process = 0,
    .ProcessHalfSec =0,
};
//-----------------------------------------------------------
uint8_t buff_Crc16Check(const uint8_t *pdata,uint8_t len);
uint8_t Buff2Uart_tx_data(const uint8_t *pdata);
// static void SwitchUserSubProcess(uint8 process);
static void Fun_Process_ON(KeyStatus_t key);
void BatteryStatus(KeyStatus_t key);
static void Fun_Process_LO(KeyStatus_t key);
static void Fun_Process_Charg(KeyStatus_t key);
static void Fun_Process_IDLE(KeyStatus_t key);
static void Fun_Process_Pumping(const KeyStatus_t* const key);
//-----------------------------------------------------------
// uint16  UartBuff_unProcessByte(uart_data_t uartData)
// {
//     uint16 retCnt;
//     if((uartData.wr - uartData.rd) > 0)
//         retCnt = uartData.wr - uartData.rd;
//     else
//         retCnt = uartData.wr + (~uartData.rd) + 1;
//     return retCnt;
// }

// void ble_rec_pro(void)
// {
//     bleuart_p12cmd_t    p_uart_data;
//     bleuart_p12event_t  bleAckCmdData;
//     uint8_t *pdata = (uint8_t *)&p_uart_data;
//     uint16_t len;
//     static uint8 recveCont = 0;
//     // static uint8 readMacIndex = 0;
//     // PeerScanInfor_t	peerInfor;
//     recveCont ++;
//     while(uart_tx_data.wr != uart_tx_data.rd)
//     {
//         if(uart_tx_data.data[uart_tx_data.rd&UART_BUF_INDEX] != P12_SYNC_CMD)
//         {
//             LOG("bad data:%02X\r\n",uart_tx_data.data[uart_tx_data.rd&UART_BUF_INDEX]);
//             uart_tx_data.rd ++;
//             continue;
//         }   

//         len = UartBuff_unProcessByte(uart_tx_data);

//         if( (len > 2) && (len > uart_tx_data.data[(uart_tx_data.rd +1)&UART_BUF_INDEX]+ 2))
//         {
//             osal_memset(&p_uart_data,0,sizeof(p_uart_data));

//             len = uart_tx_data.data[(uart_tx_data.rd +1)&UART_BUF_INDEX] + 3;       //get one siggle data len

//             pdata = (uint8_t *)&p_uart_data;
//             for(uint8_t i=0;i<len;i++){
//                 *pdata++ = uart_tx_data.data[(uart_tx_data.rd +i)&UART_BUF_INDEX];
//                 // LOG("%02X ",*(pdata-1));
//             } 
//             uart_tx_data.rd += len;

//             {
//                 bleAckCmdData.Head = P12_SYNC_EVENT;
//                 bleAckCmdData.Length = 0x02;
//                 bleAckCmdData.event = P12_EVENT_SUCCESS;
//                 bleAckCmdData.cmd = p_uart_data.cmd;  
//             }
//             if(buff_Crc16Check((uint8_t *)&p_uart_data,p_uart_data.Length+2) == *(pdata-1))
//             {
//                 UserProcess.ProcessFlag.flagBit.ReciveBle = TRUE;
//                 switch(p_uart_data.cmd)
//                 {
//                     case    P12_CMD_Sleep:
//                             // status_change = STATUS_POWEROFF;
//                             // PowerOff = TRUE;
//                             // peripheral_task_set(TASK_ACTION_START,USR_POWER_OFF,10);       // Time 2 sleep
//                             SwitchUseProcess(D_USER_PROCESS_IDLE);
//                             recveCont = 0;
//                             LOG("ble-sleep\r\n");
//                             break;
//                     case    P12_CMD_AskBleStatus:
//                             bleAckCmdData.data[0] = connect_status;
//                             bleAckCmdData.Length = 0x03;
//                             break;
//                     case    P12_CMD_RestBle:
//                             status_change = STATUS_RESET;
//                             break;
//                     case    P12_CMD_ReadBleMac:
//                             LOG("read MAC\r\n");
//                             // get_ble_mac(bleAckCmdData.data);
//                             // bleAckCmdData.Length = 0x07;
//                             break;                   
//                     case    P12_CMD_SetLedData:
//                             // osal_memcpy(CH456_DISP_BUFF,p_uart_data.data,D_DISPLAY_SIZE);
//                             // UserProcess.flag |= B_PROCESS_RcLED;
//                             if(UserProcess.process != D_USER_PROCESS_Lo && UserProcess.process != D_USER_PROCESS_IDLE)
//                             {
//                                 osal_memcpy(Driver_DisplayBuff,p_uart_data.data,sizeof(Driver_DisplayBuff)/sizeof(DisplayBufferType));
//                                 UserFlag.flagBit.miroDisplay = true;
//                             }
//                             break;
//                     case    P12_CMD_BzOnOff:
//                             UserFlag.flagBit.GetBzS = true;
//                             LOG("BZ =%d--",p_uart_data.data[0]);
//                             LOG("time =%d",p_uart_data.data[1]);
//                             LOG("on =%d",p_uart_data.data[2]);
//                             LOG("off =%d",p_uart_data.data[3]);

//                             if(p_uart_data.data[0]){
//                                 UseBzData.flag &= ~B_BZ_Disable;
//                                 if(p_uart_data.data[1])
//                                 {
//                                     UseBzData.flag |= B_BZ_RUN;
//                                     UseBzData.Count = p_uart_data.data[1];
//                                     UseBzData.OnTime = p_uart_data.data[2];
//                                     UseBzData.OffTime = p_uart_data.data[3];
//                                     Bz_init_ONOFF(UseBzData);
//                                 }
//                             }
//                             else
//                                 UseBzData.flag |= B_BZ_Disable;
//                             LOG("disable =%d",UseBzData.flag);
//                             break;
//                     case    P12_CMD_ECOMode:
//                             LOG("ECO = %d",p_uart_data.data[0]);
//                             if(p_uart_data.data[0]){
//                                 UserFlag.flagBit.EcoEn = true;
//                                 // UserFlag |= B_UserFlag_EcoEn;
//                                 // DisplayData.SymbleFlag &= ~(SYMBLE_KEY1 | SYMBLE_KEY2);  // Release off
//                             }
//                             else{
//                                 UserFlag.flagBit.EcoEn = false;
//                                 // UserFlag &= ~B_UserFlag_EcoEn;
//                                 // DisplayData.SymbleFlag |= (SYMBLE_KEY1 | SYMBLE_KEY2);  // Release on
//                             }
//                                 // peripheral_task_set(TASK_ACTION_START,USR_EVT_Display,NULL);              // update led display
//                             break;
//                     default:
//                             bleAckCmdData.Head = P12_SYNC_EVENT;
//                             bleAckCmdData.Length = 0x03;
//                             bleAckCmdData.event = P12_EVENT_CheckErr;
//                             bleAckCmdData.cmd = p_uart_data.cmd;                            
//                             break;
//                 }  
//                 // if(!(UserFlag & B_UserFlag_GetBzS))
//                 // {
//                 //     if(recveCont > 25)
//                 //     {
//                 //         recveCont = 0;
//                 //         uart_rx_data.data[(uart_rx_data.wr++)&(UART_BUF_SIZE-1)] = P12_SYNC_NOTIFY;
//                 //         uart_rx_data.data[(uart_rx_data.wr++)&(UART_BUF_SIZE-1)] = 0x02;
//                 //         uart_rx_data.data[(uart_rx_data.wr++)&(UART_BUF_SIZE-1)] = P12_CMD_BzOnOff;
//                 //         uart_rx_data.data[(uart_rx_data.wr++)&(UART_BUF_SIZE-1)] = 0x91;
//                 //         peripheral_task_set(TASK_ACTION_START,USR_UART_RCV_DATA,NULL);
//                 //     }
//                 // }            
//             }else{
//                 bleAckCmdData.event = P12_EVENT_CheckErr;
//             }  
//                 bleAckCmdData.data[bleAckCmdData.Length -2] = buff_Crc16Check((uint8 *)&bleAckCmdData,bleAckCmdData.Length + 2);
//                 // Buff2Uart_tx_data((uint8 *)&bleAckCmdData);
//                 // peripheral_task_set(TASK_ACTION_START, USR_UART_SEND_DATA, NULL);
//         }else{
//             break;
//         }
//         break;
//     }
// }

// uint8_t buff_xorCheck(const uint8_t *pdata,uint8_t len)
// {
//    uint8_t CheckSum = 0;
//    for(uint8_t i=0;i<len;i++)
//    {
//        CheckSum ^= *pdata;
//        pdata++;
//    }
//     return CheckSum;
// }

// uint8_t buff_Crc16Check(const uint8_t *pdata,uint8_t len)
// {
//     uint16_t CheckSum = 0;
//     CheckSum = crc16(0xFFFF,pdata,len);
//     return (CheckSum&0xFF);
// }


// uint8_t Buff2Uart_tx_data(const uint8_t *pdata)
// {
//     uint8_t length = 0;
//     bleuart_p12cmd_t    *p_uart_data = (bleuart_p12cmd_t *)pdata; 
//     length = p_uart_data->Length+3;
//     // LOG("TX 2 MCU:");
//     for(uint8_t i =0;i<length;i++)
//     {
//         // LOG("%X ",*pdata);        
//         uart_tx_data.data[(uart_tx_data.wr++)&(UART_BUF_SIZE-1)] = *pdata++;
//     }
//     // LOG("\n");
//     return 0;
// }
// //---------------------------------------------------------------------------------------------------------------------------
static void DisplayConnect(void);
static void DisplayBattery(void);
void SwitchUseProcess(USER_PROCESS process)
{

    if (process > D_USER_PROCESS_IDLE)
    {
        LOG("Switch process out range\r\n");
        process = D_USER_PROCESS_IDLE;
    }
    if (UserProcess.process != process)
    {
        UserProcess.process = process;
        UserProcess.ProcessFlag.flagBit.run = 0;
        UserProcess.ProcessFlag.flagBit.BackLight = 1;  // 开启背光
    }
}

void SwitchUserSubProcess(uint8 process)
{
    UserProcess.subProcess = process;
    UserProcess.ProcessFlag.flagBit.Subrun = 0;
}
void SwitchUserSubProcessNext(void)
{
    SwitchUserSubProcess(UserProcess.subProcess + 1);
}
ClockFlag_t UserProcessColcok(void)
{
    uint32_t now_clock_tick = hal_systick();
    static uint32_t old_clock_tick = 0;
    static uint8 tickCnt = 0;
    uint32 subClockCnt = 0;
    ClockFlag_t clockFlag={
        .flagByte = 0,
    };
    if (now_clock_tick > old_clock_tick)
    {
        subClockCnt = now_clock_tick - old_clock_tick;
    }
    else
    {
        subClockCnt = now_clock_tick + (~old_clock_tick) + 1;
        // if(testUse & 0x02)
        //     HalGpioSet(GREENLITE_PIN,Bit_DISABLE);
        // else
        //     HalGpioSet(GREENLITE_PIN,Bit_ENABLE);
        LOG("now tick = %d,old tick =%d\r\n", now_clock_tick, old_clock_tick);
    }
    subClockCnt = subClockCnt * 625 / 1000; // Cnt 2 ms
    if (subClockCnt > 16)
    {
        clockFlag.flagBit._16ms = 1;
        tickCnt++;
        if (tickCnt & 0x01)
            clockFlag.flagBit._32ms = 1;
        if ((tickCnt & 0x03) == 0x03)
            clockFlag.flagBit._64ms = 1;
        if ((tickCnt & 0x07) == 0x07)
            clockFlag.flagBit._128ms = 1;
        if ((tickCnt & 0x0F) == 0x0F)
            clockFlag.flagBit._256ms = 1;
        if ((tickCnt & 0x1F) == 0x1F)
            clockFlag.flagBit.HalfSecond = 1;
        if ((tickCnt & 0x3F) == 0x3F)
            clockFlag.flagBit.Second = 1;
        if ((tickCnt & 0x7F) == 0x7F)
            clockFlag.flagBit._2Second = 1;
        old_clock_tick += (16 / 1000 * 1600);
    }
    return clockFlag;
}


// #include "uart_process.h"
void FunUserProcess(void)
{
    static KeyStatus_t KeyData={
        .Cont =0,
        .LongFastTrage = 0,
        .LongTrage = 0,
        .ReleaseTrage = 0,
        .ShortTrage = 0,
        .Trage = 0,
    }; // = KeyScan();
    static uint8_t ScanTimeCnt = 0;
    User_BZ_t bzData;

    UserProcess.clockflag = UserProcessColcok();
    if (UserProcess.clockflag.flagBit.HalfSecond)
    {
        UserProcess.ProcessHalfSec++;
        UserProcess.subProcessHalfSec++;
        if(UserProcess.ProcessFlag.flagBit.FBDisplayFlash1S)
            UserProcess.ProcessFlag.flagBit.FBDisplayFlash1S = 0;
        else
        {
            UserProcess.ProcessFlag.flagBit.DisplayFlash = (!UserProcess.ProcessFlag.flagBit.DisplayFlash);
        }
    }
    if(UserProcess.clockflag.flagBit._32ms)
    {
        KeyData.Trage = GetKeyBits();
        KeyScan(&KeyData);
        // if(!adv_en)
        // {
        //     if(KeyData.Cont.flagBit.onOff && KeyData.ReleaseTrage.flagBit.Tare && KeyData.ClikckCnt[TATE_BIT_PLASE] == 2)
        //     {
        //         adv_en = TRUE;
        //         ble_task_set(TASK_ACTION_START,SBP_RESET_ADV_EVT,NULL);
        //     }
        // }
        #if(0)
            {
                if(KeyData.Trage.flagByte)
                    LOG("keyTrage:%02X\r\n",KeyData.Trage.flagByte);
                if(KeyData.LongFastTrage.flagByte)
                    LOG("long fast keyTrage:%02X\r\n",KeyData.LongFastTrage.flagByte);
                if(KeyData.LongTrage.flagByte)
                    LOG("long keyTrage:%02X\r\n",KeyData.LongTrage.flagByte);
                if(KeyData.ShortTrage.flagByte)
                    LOG("Short keyTrage:%02X\r\n",KeyData.ShortTrage.flagByte);
                if(KeyData.ReleaseTrage.flagByte)
                {
                    LOG("key release trage:%02X\r\n",KeyData.ReleaseTrage.flagByte);
                    LOG_DUMP_BYTE(KeyData.ClikckCnt,sizeof(KeyType_t)*8);
                }
            }
        #endif
        if((KeyData.Trage.flagByte & USEED_KEY_BIT) && (UserProcess.process != D_USER_PROCESS_IDLE) && (UserProcess.process != D_USER_PROCESS_Charg))
        {
            osal_memset(&bzData,0,sizeof(bzData));
            UseBzData.flag |= B_BZ_KeyOn;
            bzData.flag = B_BZ_RUN;
            bzData.Count = 1;
            // bzData.OnTime = 1000;   //1.5s
            Bz_init_ONOFF(bzData);
        }
        // if(!(KeyData.Cont.flagByte & USEED_KEY_BIT) && (UseBzData.flag & B_BZ_KeyOn))
        // {
        //     osal_memset(&bzData,0,sizeof(bzData));
        //     Bz_init_ONOFF(bzData);      // 释放 关闭 蜂鸣器
        // }

        ScanTimeCnt++;
        if (!KeyData.Cont.flagBit.Charg && !KeyData.Cont.flagBit.ChargFull)
        {
            Battery_t.Flag.flagBit.Chage = 0;
            Battery_t.Flag.flagBit.ChageFull = 0;
    #if ENable_BAT_LO
            if (!(Battery_t.Flag.flagBit.init))
                ScanTimeCnt = 0;
            if(Battery_t.Flag.flagBit.enableBatLo)
                FunUserADC();
            if (Battery_t.Flag.flagBit.OK)
            {
                if (ScanTimeCnt > 0x80)
                {
                    ScanTimeCnt = 0;
                    // Battery_t.flag = 0; // 重新测量电池电压
                    Battery_t.Flag.flagByte = 0; // 重新测量电池电压
                    Battery_t.Flag.flagBit.enableBatLo = TRUE;
                    Battery_t.Flag.flagBit.Finish1S = 1;
                }
            }
            // if (Battery_t.Flag.flagBit.OK && Battery_t.Flag.flagBit.LO && UserProcess.process != D_USER_PROCESS_IDLE)
            // {
            //     SwitchUseProcess(D_USER_PROCESS_Lo);
            // }
    #endif
        }
        else
        {
            if(UserProcess.ProcessFlag.flagBit.WeakUp && D_USER_PROCESS_IDLE != UserProcess.process && D_USER_PROCESS_PUMPING != UserProcess.process)         //首先完成设备初始化方可进入充电进程
                SwitchUseProcess(D_USER_PROCESS_Charg);
            Battery_t.Flag.flagBit.init = 0;
            Battery_t.Flag.flagBit.Chage = 1;
            // Battery_t.Flag.flagBit.OK = 1;
            Battery_t.Flag.flagBit.Finish1S = 1;
            if(!Battery_t.Flag.flagBit.ChageFull)
            {
                if ((KeyData.Cont.flagBit.ChargFull) || UserProcess.ProcessHalfSec > 14400)    //充电时间超过 2hour 默认充满
                {
                    Battery_t.Flag.flagBit.ChageFull = 1;// 一次充满全当充满
                    LOG("charg full time=%d\n",UserProcess.ProcessHalfSec);
                }
            }
        }

        if (!(UserProcess.ProcessFlag.flagBit.run) || (KeyData.ShortTrage.flagByte&USEED_KEY_BIT) || (KeyData.Trage.flagByte&USEED_KEY_BIT))
        {
            UserProcess.ProcessHalfSec = 0;
            UserProcess.ProcessFlag.flagBit.DisplayFlash = 1;
            UserProcess.ProcessFlag.flagBit.FBDisplayFlash1S = 1;
        }

        if (1 && DEBUG_INFO)
        {
            if (!(UserProcess.ProcessFlag.flagBit.run))
            {
                LOG("----------process step = %d--------------flag = %X\r\n", UserProcess.process,UserProcess.ProcessFlag.flagByte);
            }
        }
        switch(UserProcess.process)
        {
            case    D_USER_PROCESS_ON:
                    Fun_Process_ON(KeyData);
                    break;
            case    D_USER_PROCESS_PUMPING:
                    Fun_Process_Pumping(&KeyData);
                    break;
            case    D_USER_PROCESS_Charg:
                    Fun_Process_Charg(KeyData);
                    break;
            case    D_USER_PROCESS_Lo:
                    Fun_Process_LO(KeyData);
                    break;
            case    D_USER_PROCESS_IDLE:
                    // UserFlag &= ~B_UserFlag_MiroDisplay;
                    // bzData.flag = 0;
                    // Bz_init_ONOFF(bzData);
                    // Tm1640_IO_PowerDown();
                    Fun_Process_IDLE(KeyData);
                    break;
            default:
                    break;
        }
        if(UserProcess.ProcessFlag.flagBit.WeakUp)
            DisplayBattery();
        HalDisplayEvtProcss();
    }
}

static void Fun_Process_Pumping(const KeyStatus_t* const key)
{
    // User_BZ_t bzData;
    if(!(UserProcess.ProcessFlag.flagBit.run))
    {
        UserProcess.ProcessFlag.flagBit.run = TRUE;
        displayClrBuf(&DisplayData);
        HalGpioSet(PUMP_Ctl_PIN,Bit_ENABLE);
    }
    if(key->ShortTrage.flagBit.onOff || UserProcess.ProcessHalfSec > 120)       //手动关机或1分钟超时关机
    {
        SwitchUseProcess(D_USER_PROCESS_IDLE);
        HalGpioSet(PUMP_Ctl_PIN,Bit_DISABLE);
    }
}

static void Fun_Process_IDLE(KeyStatus_t key)
{
    User_BZ_t bzData;
    if(!(UserProcess.ProcessFlag.flagBit.run))
    {
        UserProcess.ProcessFlag.flagBit.run = TRUE;
        osal_memset(&bzData,0,sizeof(bzData));
        if(UserProcess.ProcessFlag.flagBit.handleOFF)
        {
            bzData.flag = 0;
            bzData.flag = B_BZ_RUN;
            bzData.Count = 1;
            Bz_init_ONOFF(bzData);
        }else{
            Bz_init_ONOFF(bzData);
            UserProcess.ProcessFlag.flagBit.idle = TRUE;
        }
        UserProcess.ProcessFlag.flagBit.handleOFF = false;
        displayClrBuf(&DisplayData);
        HalGpioSet(PUMP_Ctl_PIN,Bit_DISABLE);
    }
    {
        // UserFlag &= ~B_UserFlag_MiroDisplay;
        UserFlag.flagBit.miroDisplay = false;
        UserProcess.ProcessFlag.flagBit.WeakUp = false;
        if(Battery_t.Flag.flagBit.Chage)
            SwitchUseProcess(D_USER_PROCESS_Charg);
        else
        {
            if(!(UseBzData.flag&B_BZ_RUN) || UserProcess.ProcessHalfSec > 2)
            {
                UserProcess.ProcessFlag.flagBit.idle = TRUE;
                useApp_task_set(USER_TASK_START,UserAppEvent_POWER_OFF,16);
            }
        }
    }
}

static void Fun_Process_ON(KeyStatus_t key)
{
    KeyFlag_t tempKey = {
        .flagByte = 0,
    };
    uint8_t click_times = 0;
    if(!(UserProcess.ProcessFlag.flagBit.run))
    {
        UserProcess.ProcessFlag.flagBit.run = TRUE;
        DisplayDriverInit();
        // adcMeasureTask();
        // SwitchUserSubProcess(0);
        UserProcess.ProcessFlag.flagBit.Subrun = TRUE;
        useApp_task_set(USER_TASK_START,UserAppEvent_Adc,10);
        UserProcess.ProcessFlag.flagBit.WeakUp = true;

        HalGpioSet(PUMP_Ctl_PIN,Bit_DISABLE);
        HalGpioPinInit(PUMP_Ctl_PIN, GPIO_OUTPUT);
        HalGpioPupdConfig(PUMP_Ctl_PIN, GPIO_FLOATING);
    }
    if (1 && DEBUG_INFO)
    {
        if (!(UserProcess.ProcessFlag.flagBit.Subrun))
        {
            LOG("----------sub process step = %d--------------flag = %X\r\n", UserProcess.subProcess,UserProcess.ProcessFlag.flagByte);
        }
    }
    if(key.ReleaseTrage.flagBit.onOff)
    {
        tempKey.flagBit.onOff = 1;
        click_times = CheckKeyClickTimes(tempKey,&key);
        LOG("click times = %d\r\n",click_times);
        if(click_times == 2)
        {
            SwitchUseProcess(D_USER_PROCESS_PUMPING);
        }else{
            SwitchUseProcess(D_USER_PROCESS_IDLE);
        }
    }else{
        if(UserProcess.ProcessHalfSec > 2)
        {
            SwitchUseProcess(D_USER_PROCESS_IDLE);
        }
    }
}


static void DisplayConnect(void)
{
    DisplayData.SymbleFlag.bit.connect = false;
    if(connect_status)
        DisplayData.SymbleFlag.bit.connect = true;
}

static void DisplayBattery(void)
{

    if(Battery_t.Flag.flagBit.Chage)
    {
        if(Battery_t.Flag.flagBit.ChageFull)
        {
            DisplayData.SymbleFlag.bit.green = 1;
        }else{
            DisplayData.SymbleFlag.bit.red = 1;
        }
    }else{
        if(Battery_t.Flag.flagBit.OK)
        {
            if(Battery_t.BatteryLevle > 30)
                DisplayData.SymbleFlag.bit.green = 1;
            else
                DisplayData.SymbleFlag.bit.red = 1;
        }
    }
}


static void Fun_Process_Charg(KeyStatus_t key)
{
    KeyFlag_t tempKey = {
        .flagByte = 0,
    };
    static uint8_t unChargCnt = 0;
    if(!(UserProcess.ProcessFlag.flagBit.run))
    {
        UserProcess.ProcessFlag.flagBit.run = TRUE;
        UserProcess.ProcessFlag.flagBit.WeakUp= TRUE;
        UserProcess.ProcessFlag.flagBit.PowerON = false;
        Battery_t.Flag.flagBit.ChageFull = 0;
        Battery_t.Flag.flagBit.Chage = TRUE;
        if(!UserProcess.clockflag.flagBit.HalfSecond)
        {
            UserProcess.clockflag.flagBit.HalfSecond = TRUE;
            DisplayBattery();
            UserProcess.clockflag.flagBit.HalfSecond = FALSE;
        }
        unChargCnt = 0;
    }
    displayClrBuf(&DisplayData);

    if(key.ReleaseTrage.flagBit.onOff)
    {
        tempKey.flagBit.onOff = 1;
        if(CheckKeyClickTimes(tempKey,&key) == 2)
        {
            SwitchUseProcess(D_USER_PROCESS_PUMPING);
        }
    }

    if(UserProcess.clockflag.flagBit.HalfSecond)
    {
        LOG("Charg flag =%X\n",Battery_t.Flag.flagByte);
    }

    if(!key.Cont.flagBit.Charg && !key.Cont.flagBit.ChargFull)
    {
        unChargCnt++;
        if(unChargCnt > 10)
        {
            unChargCnt = 0;
            SwitchUseProcess(D_USER_PROCESS_IDLE);
        }
    }else{
        unChargCnt = 0;
    }
}


static void Fun_Process_LO(KeyStatus_t key)
{
    if(!(UserProcess.ProcessFlag.flagBit.run))
    {
        UserProcess.ProcessFlag.flagBit.run = TRUE;
    }    
    if(UserProcess.ProcessHalfSec > 4)      //2s休眠
    {
        SwitchUseProcess(D_USER_PROCESS_IDLE);
    }
    // UserFlag &= (~B_UserFlag_MiroDisplay);
    UserFlag.flagBit.miroDisplay = false;
    displayClrBuf(&DisplayData);
    displayChar2Tuble("Lo",DisplayData.Data,2);
    DisplayConnect();
}



void Buff2UartStructdata(uart_data_t *Structdata,const uint8_t *data,uint16_t len)
{
    if(!Structdata || !data || !len)
        return;
    for(uint8_t i = 0;i<len;i++)
        Structdata->data[(Structdata->wr++)&(UART_BUF_SIZE-1)] = *data++;
}



